package main.controller;

import java.util.List;

import main.entity.ChannelVO;
import main.entity.ThreadsVO;
import main.service.ChannelService;
import main.service.ThreadsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/channels")
public class ChannelCtrl extends BaseCtrl<ChannelVO> {

	@Autowired
	@Qualifier("channelService")
	private ChannelService channelService;
	@Autowired
	@Qualifier("threadsService")
	private ThreadsService threadsService;
	
	ChannelCtrl(){
		targetURL = "";
	}
	
	@RequestMapping("/all")
//	@ResponseBody
	public ModelAndView getChannels(){
		List<ChannelVO> allChannels = channelService.getAllChannels();
//		request.setAttribute("allChannels", allChannels);
		ModelAndView mdv = new ModelAndView();
		
		targetURL = "/channels/all";
		mdv.addObject("allChannels", allChannels);
		mdv.addObject("targetURL", targetURL);
//		return targetURL;
		return mdv;
	}
	
	
	@RequestMapping("/{channelID}")
//	@ResponseBody
	public ModelAndView getThreadsByID(@PathVariable("channelID") Integer channelID){
		List<ThreadsVO> threadsByChannel = threadsService.getByChannelID(channelID);
//		request.setAttribute("threadsByChannel", threadsByChannel);
		targetURL="/"+channelID;
		mdv.setViewName(targetURL);
		mdv.addObject("threadsByChannel", threadsByChannel);
		return mdv;
	}
	
}
